<?php

namespace app\common\model\lease;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'lease_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'paytime_text',
        'image_str',
        'images_array',

    ];
    public function getImageStrAttr($value, $data)
    {
        if (isset($data['image']) && $data['image']) {
            return cdnurl($data['image'], true);
        }
        return '';
    }
    public function getImagesArrayAttr($value, $data)
    {
        if (isset($data['images']) && $data['images']) {
            $images = explode(',', $data['images']);
            foreach ($images as &$v) {
                $v = cdnurl($v, true);
            }
            return $images;
        }
        return [];
    }
    public function getCancelStatusList()
    {
        return [  '2' => __('已审核'), '3' => __('未通过')];
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '-1' => __('Status -1')];
    }

    public function getCancelStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cancel_status']) ? $data['cancel_status'] : '');
        $list = $this->getCancelStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function lease()
    {
        return $this->belongsTo('app\common\model\lease\Lease', 'l_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
